package org.extframework.ld.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Random;

/**
 * @author : Dong.Li
 * @created : 2013-1-17
 * @version : 1.0
 * @description :
 */
public class DateUtil {

	private static final String[] WEEKS = { "日", "一", "二", "三", "四", "五", "六" };
	public static SimpleDateFormat ymd_hms = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	public static SimpleDateFormat ymdhms = new SimpleDateFormat("yyMMddHHmmssSSS");
	public static SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
	private static String hms = "HH:mm:ss";
	private static DateFormat df_hm = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	private static DateFormat yyyymmddd = new SimpleDateFormat("yyyyMMdd");
	private static Calendar cale = Calendar.getInstance();
	private static Random random = new Random();
	

	public static String getChineseTime() {
		Calendar c = Calendar.getInstance();
		int year = c.get(1);
		int mouth = c.get(2);
		int day = c.get(5);
		int week = c.get(7);
		return year + "年" + (mouth + 1) + "月" + day + "日" + " 星期"
				+ WEEKS[(week - 1)];
	}

	public static String getWeek(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);

		return new SimpleDateFormat("EEEE").format(c.getTime());
	}
	
	 /**
		 * 设定一个日期时间，加几分钟后得到新的日期
		 * @param day
		 * @param x
		 * @return
		 */
		 public static Date addDateMinut(Date day, int x)//返回的是字符串型的时间，输入的
		 {   
		        Date date = day;   
		        if (date == null)   
		            return null;   
		        Calendar cal = Calendar.getInstance();   
		        cal.setTime(date);   
		        cal.add(Calendar.MINUTE, x);// 24小时制   
		        date = cal.getTime();   
		        cal = null;   
		        return date;  
		  
		    }
		 /**
			 * 设定一个日期时间，加几天后得到新的日期
			 * @param day
			 * @param x
			 * @return
			 */
			 public static Date addDateDay(Date day, int x)//返回的是字符串型的时间，输入的
			 {   
			        Date date = day;   
			        if (date == null)   
			            return null;   
			        Calendar cal = Calendar.getInstance();   
			        cal.setTime(date);   
			        cal.add(Calendar.MINUTE, x*1440);// 24小时制   
			        date = cal.getTime();   
			        cal = null;   
			        return date;  
			  
			    }

	public static Boolean isSameDay(Date d1, Date d2) {
		Calendar cal = new GregorianCalendar();
		cal.setTime(d1);
		int id1 = cal.get(6);
		int iy1 = cal.get(1);
		cal.setTime(d2);
		int id2 = cal.get(6);
		int iy2 = cal.get(1);

		if ((id1 == id2) && (iy1 == iy2))
			return Boolean.valueOf(true);
		return Boolean.valueOf(false);
	}

	public static String format(Date date, String format) {
		DateFormat df = new SimpleDateFormat(format);
		return df.format(date);
	}

	public static Date getDate(String sDate, String format) {
		DateFormat df = new SimpleDateFormat(format);
		try {
			return df.parse(sDate);
		} catch (ParseException e) {
			throw new RuntimeException("date format error");
		}
	}

	public static String formatDateDayHs(Date date) {
		return ymd_hms.format(date);
	}

	public static String formatDateDay(Date date) {
		DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
		return df.format(date);
	}

	public static Date getDateDay(String sDate) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return df.parse(sDate);
		} catch (ParseException e) {
			throw new RuntimeException("date format error");
		}
	}
	
	public static String getDateHmStr(Date date){
		return df_hm.format(date);
	}
	
	public static Date getDateHm(String hmDate){
		try {
			return df_hm.parse(hmDate);
		} catch (ParseException e) {
			e.printStackTrace();
			throw new RuntimeException("date format error");
		}
	}

	public static String getDateYYYYMMDD( Date date ){
		return yyyymmddd.format(date);
	}
	
	public static Date getDateYYYYMMDD( String date ){
		try {
			return yyyymmddd.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			throw new RuntimeException("date format error");
		}
	}
	
	public static Date getDate(String sDate) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return df.parse(sDate);
		} catch (ParseException e) {
			throw new RuntimeException("date format error");
		}
	}

	public static Date getYmdDate(String sDate) {
		try {
			return ymd.parse(sDate);
		} catch (ParseException e) {
			throw new RuntimeException("date format error");
		}
	}

	public static String getDateTime(Date date) {
		try {
			return ymd.format(date);
		} catch (Exception e) {
		}
		return "";
	}
	
	public static String getDateTimeToFileName() {
		try {
			return ymdhms.format(cale.getTime())+"_"+Math.abs(random.nextLong());
		} catch (Exception e) {
		}
		return "";
	}

	public static String getDateTime() {
		try {
			return ymd_hms.format(cale.getTime());
		} catch (Exception e) {
		}
		return "";
	}

	public static String formatDateHMS(Date date) {
		try {
			return format(date, hms);
		} catch (Exception e) {
		}
		return null;
	}

}
